﻿using Basic.HrModel.DB;
using Basic.HrModel.EmpPost;
using WeDonekRpc.Helper.IdGenerator;
using WeDonekRpc.SqlSugar;

namespace Basic.HrDAL.Repository
{
    internal class EmpPostDAL : BasicDAL<DBEmpPost, long>, IEmpPostDAL
    {
        public EmpPostDAL ( IRepository<DBEmpPost> basicDAL ) : base(basicDAL)
        {
        }

        public long Add ( EmpPostAddDto add )
        {
            DBEmpPost data = new DBEmpPost
            {
                DeptId = add.DeptId,
                CompanyId = add.CompanyId,
                EmpId = add.EmpId,
                Id = IdentityHelper.CreateId(),
                PostCode = add.PostCode,
                UnitId = add.UnitId,
            };
            this._BasicDAL.Insert(data);
            return data.Id;
        }
        public void Clear ( long empId )
        {
            _ = this._BasicDAL.Delete(a => a.EmpId == empId);
        }
        public Dictionary<long, string[]> GetEmpPost ( KeyValuePair<long, long>[] empAndDept )
        {
            return this._BasicDAL.Gets(a => empAndDept.Any(s => a.EmpId == s.Key && a.DeptId == s.Value), a => new
            {
                a.EmpId,
                a.PostCode
            }).GroupBy(a => a.EmpId).ToDictionary(a => a.Key, a => a.Select(c => c.PostCode).ToArray());
        }
    }
}
